//
//  AlertControllAlertView.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/10.
//

#import "AlertControllAlertView.h"
#import "ZJPendApprovalDictModel.h"
#import "AlertControllerTableViewCell.h"


@interface AlertControllAlertView () <UITableViewDelegate, UITableViewDataSource>{
    NSInteger Index;
    UIView * topV;
}

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * chooseData;

@end

@implementation AlertControllAlertView



- (instancetype)init
{
    self = [super initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        
        topV = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT , SCREEN_WIDTH, 40)];
        topV.backgroundColor = COLOR_RGBA(242, 242, 242, 1.0);
        [self addSubview:topV];

        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0, 100, 40);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [cancelBtn addTarget:self action:@selector(respondsTo_checkCancel) forControlEvents:UIControlEventTouchUpInside];
        [topV addSubview:cancelBtn];

        UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        yesBtn.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 100, 40);
        [yesBtn setTitle:@"完成" forState:UIControlStateNormal];
        [yesBtn setTitleColor:COLOR_RGBA(69, 186, 155, 1) forState:UIControlStateNormal];
        [yesBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [yesBtn addTarget:self action:@selector(respondsTo_checkyes) forControlEvents:UIControlEventTouchUpInside];
        [topV addSubview:yesBtn];

        self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        self.tableView.frame = CGRectMake(12, CGRectGetMaxY(topV.frame), SCREEN_WIDTH - 24, 207);
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];

        
        [UIView animateWithDuration:0.25 animations:^{
            topV.frame = CGRectMake(0, SCREEN_HEIGHT - 247, SCREEN_WIDTH, 40);
            self.tableView.frame = CGRectMake(0, CGRectGetMaxY(topV.frame), SCREEN_WIDTH, 207);
        }];
        
    }
    return self;
}


-(void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = [ZJPendApprovalDictModel mj_objectArrayWithKeyValuesArray:dataSource];
    [self.tableView reloadData];
}




-(void)respondsTo_checkCancel{
    [self remove];
}
-(void)respondsTo_checkyes{
    [self remove];
    NSString *timeStr;
    NSMutableArray *selectedArray = [NSMutableArray array];
    for (ZJPendApprovalDictModel *model in self.dataSource) {
        if (model.Isselect) {
            [selectedArray addObject:model.value];
        }
    }
    timeStr = [selectedArray componentsJoinedByString:@","];

    _block(timeStr);
    
}


- (void)remove {

    [UIView animateWithDuration:0.25 animations:^{

        self->topV.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 40);
        self.tableView.frame = CGRectMake(0, CGRectGetMaxY(self->topV.frame), SCREEN_WIDTH, 207);

    } completion:^(BOOL finished) {

        [self removeFromSuperview];
    }];

}

#pragma amrk -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AlertControllerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AlertControllerTableViewCell"];
    if (!cell){
        cell = [[AlertControllerTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"AlertControllerTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ZJPendApprovalDictModel * model = self.dataSource[indexPath.row];
    if (model.Isselect){
        cell.img.image = [UIImage imageNamed:@"对"];
    }else{
        cell.img.image = [UIImage imageNamed:@""];
    }
    
    cell.model = model;
    
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ZJPendApprovalDictModel * model = self.dataSource[indexPath.row];
    AlertControllerTableViewCell * cell = (AlertControllerTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

        // 多选
        if (model.Isselect){ // 选中
            model.Isselect = NO;
            [self.chooseData removeObject:model.value];
        }else{ // 未选
            model.Isselect = YES;
            [self.chooseData addObject:model.value];
        }
        
        [self.tableView reloadData];
        
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(45);
}


@end
